package adata;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

import model.ApplicationModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.ApplicantForm;
import forms.Degree;

public class AddressPageAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException {
			
		ApplicantForm ad = (ApplicantForm) form;
		
		String cc = ad.getCountrycode();
		request.getSession().setAttribute("countrycode", cc);
		
		
		String state = ad.getState();
		request.getSession().setAttribute("state", state);
		
		
		
		
		RowSet crsLocations = ApplicationModel.getAllLocations();
		
		// Store the RowSet in the request scope
		request.setAttribute("crsLocations", crsLocations);

		
		
		// COUNTER UPDATE
		Integer counter = (Integer) (request.getSession().getAttribute ("SessionCounter"));
		// if it is the first time this page has been visited, set counter to 0
		if (counter == null)  {
			counter = new Integer(0);
		}
		// else not the first visit, increment counter by 1
		else  {
			counter = new Integer(counter.intValue() + 1);
		}
		
		request.getSession().setAttribute("SessionCounter", counter);
		
		
		
		
		// if US citizen, direct to degreeLocationPage.jsp
		if (ad.getCountry1().equals("United States") || (counter > 0))  {
			return mapping.findForward("success-US");
		}
		// else not US citizen, direct to residencyPage.jsp
		else  {
			return mapping.findForward("success");
		}
		
	}
}